package com.cskaoyan.controller;

import com.cskaoyan.module.bean.BaseRespVo;

import com.cskaoyan.module.bean.MarketCouponInfo;
import com.cskaoyan.module.bean.param.BaseParam;
import com.cskaoyan.module.bo.CouponCreateParamBo;
import com.cskaoyan.module.bo.CouponListParamsBo;
import com.cskaoyan.module.bo.CouponListUserParamBo;
import com.cskaoyan.module.pojo.MarketCoupon;
import com.cskaoyan.module.vo.MarketCouponVo;
import com.cskaoyan.service.PopularizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * PopularizeController
 *
 * @param
 * @return
 * @2022/4/6
 **/
@RestController
@RequestMapping("admin/coupon")
public class PopularizeController {


    @Autowired
    @Qualifier("popularizeServiceImpl")
    PopularizeService popularizeService;
    @RequestMapping("list")
    public BaseRespVo<MarketCouponVo> list(CouponListParamsBo params,Integer limit) {
        MarketCouponVo marketCouponVo=null;
        BaseRespVo ok=null;
        if (limit == 0) {
//            优惠卷不限制 ->根据limit=0查询
            marketCouponVo=popularizeService.listLimit0(0);

        } else if (params == null || "".equals(params)) {
//            查询所有的优惠卷信息
            marketCouponVo=popularizeService.listAll();
        } else {

            marketCouponVo = popularizeService.list(params);
        }
        if (marketCouponVo != null) {
            return BaseRespVo.ok(marketCouponVo);
        } else {
            return BaseRespVo.invalidData("更新优惠卷出错！");
        }
    }

    @RequestMapping("create")
    public BaseRespVo<MarketCoupon> create(@RequestBody CouponCreateParamBo params) {
        MarketCoupon marketCoupon=null;
        System.out.println("create params = " + params);

        marketCoupon = popularizeService.create(params);
        if (marketCoupon == null) {
            return  BaseRespVo.invalidData("服务器出错！");
        }
//        参数接收处理！ 如果出错要按格式提示!
        BaseRespVo ok = new BaseRespVo();
        ok.setErrmsg("成功");
        ok.setData(marketCoupon);
        System.out.println("ok = " + ok);
        return  ok;
    }
    @RequestMapping("listuser")
    public BaseRespVo<MarketCouponVo> listUser( BaseParam baseParam,Integer couponId,Integer userId,Integer status) {
        MarketCouponVo marketCoupon=null;
        System.out.println("listUser baseParam = " + baseParam);

        CouponListUserParamBo params = new CouponListUserParamBo();
        params.setLimit(baseParam.getLimit());
        params.setOrder(baseParam.getOrder());
        params.setPage(baseParam.getPage());
        params.setSort(baseParam.getSort());

        if (couponId != null) {

            params.setCouponId(couponId);

        }
        if (userId != null) {
            params.setUserId(userId);
        }else {
            params.setUserId(-1);
        }
        if (status != null) {
            params.setStatus(status);
        } else {
            params.setStatus(-1);
        }
        marketCoupon = popularizeService.listUser(params);

        BaseRespVo ok = new BaseRespVo();
        ok.setErrmsg("成功");
        ok.setData(marketCoupon);
        System.out.println("ok = " + ok);

        return  ok;
    }
    @RequestMapping("update")
    public BaseRespVo<MarketCouponInfo> update(@RequestBody MarketCoupon  params) {

        System.out.println("update params = " + params);

        MarketCoupon marketCoupon =new MarketCoupon();
        marketCoupon=popularizeService.update(params);
        System.out.println("update marketCoupon="+marketCoupon);
        if (marketCoupon != null) {
            return BaseRespVo.ok(marketCoupon);
        } else {
            return BaseRespVo.invalidData("更新优惠卷出错！");
        }

    }
    @RequestMapping("delete")
    public BaseRespVo<MarketCouponInfo> delete(@RequestBody MarketCoupon  params) {
        MarketCouponInfo marketCoupon=null;
        System.out.println("delete params = " + params);


        int code = popularizeService.delete(params);

        if (code == 0) {
            return BaseRespVo.invalidData("删除优惠卷失败！");
        } else {
            return  BaseRespVo.ok("成功");
        }
    }

    @RequestMapping("read")
    public BaseRespVo<MarketCouponInfo> read(Integer  id) {
        MarketCouponInfo marketCoupon=null;
        System.out.println("delete params = " + id);

        marketCoupon = popularizeService.read(id);

        if (marketCoupon == null) {
            return BaseRespVo.invalidData("读取优惠卷详情失败！");
        } else {

            return BaseRespVo.ok(marketCoupon);
        }
    }
}
